package com.example.musicplayer;

import android.content.Context;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;

public class Properbean {

    public static String[] THEMES;
    private static String DEFAULT_THEME;
    private Context context;
    private String theme;

    public Properbean(Context context){
        this.context = context;
        THEMES= context.getResources().getStringArray(R.array.theme);
        DEFAULT_THEME = THEMES[0];
        this.loadTheme();
    }

    /**
     * 加载主题，主题本地化
     */
    private void loadTheme(){
        Properties properties = new Properties();
        try {
            FileInputStream stream = context.openFileInput("configuration.cfg");
            //使用properties加载输入流
            properties.load(stream);
            theme = properties.getProperty("theme").toString();
        }catch (Exception e){
            this.saveTheme(DEFAULT_THEME);
        }
    }

    /**
     * 将主题加入到输出流
     * @param theme
     * @return boolean
     */
    private boolean saveTheme(String theme){
        Properties properties = new Properties();
        properties.put("theme",theme);
        try{
            FileOutputStream stream = context.openFileOutput("configuration.cfg",context.MODE_PRIVATE);
            properties.store(stream,"");
            return true;
        }catch (Exception e){
            return false;
        }
    }

    public String getTheme() {
        return theme;
    }

    /**
     * 设置并保存主题
     * @param theme
     */
    public void setAndSaveTheme(String theme){
        this.theme = theme;
        this.saveTheme(theme);
    }
}
